﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
using ZhCun.Utils.Helpers;
using ZhCun.WebUtils;

namespace ZhCun.RightsManage.ApiControllers
{
    [UserAuthorize(false)]
    public class ApiTestController : BaseApiController
    {
        /// <summary>
        /// 根据铭文内容获取密文key及加密body
        /// </summary>
        /// <param name="key">明文密钥</param>
        /// <param name="body">明文body内容</param>
        /// <returns>返回加密后内容</returns>
        [BodyEncryptFilter(false)]
        [HttpPost]
        public string GetEncryptBody(string key, string body)
        {
            string rsaKey = System.IO.File.ReadAllText("body.key");
            var rsa = new RSAHelper(rsaKey);
            var ciphertextBody = DEncryptAES.Encrypt(body, key);
            var ciphertextKey = rsa.Encrypt(key);
            return $"{ciphertextBody} , {ciphertextKey}";
        }
        /// <summary>
        /// 获取明文内容
        /// </summary>
        /// <param name="key">加密的key</param>
        /// <param name="body">加密的body</param>
        /// <param name="keyType">密钥类型 0:明文 1：密文</param>
        /// <returns>返回明文</returns>
        [BodyEncryptFilter(false)]
        [HttpPost]
        public string GetDecryptBody(int keyType, string key, string body)
        {
            if (keyType == 1)
            {
                string rsaKey = System.IO.File.ReadAllText("body.key");
                var rsa = new RSAHelper(rsaKey);
                key = rsa.Decrypt(key);
            }
            var plaintextBody = DEncryptAES.Decrypt(body, key);
            return plaintextBody;
        }


        [HttpGet]
        public string Test1()
        {
            return $"x现在时间: {DateTime.Now} , 你的IP为：{RemoteIp}";
        }
        [HttpGet]
        public object Test2()
        {
            var fullPath = System.Reflection.Assembly.GetEntryAssembly().Location;
            Path.GetDirectoryName(fullPath);
            return $"Location ：{fullPath}";
        }

        [HttpPost]
        [BodyEncryptFilter]
        public string GetTest(Arg1 arg)
        {
            return $"收到文本为:{arg.Text}";
        }
    }
    public class Arg1
    {
        public string Text { set; get; }
    }


}